//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cy.src.update.ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.cy.lib.download.DownloadService;
import com.cy.lib.download.Download.DownloadInfo;
import com.cy.lib.download.Download.DownloadState;
import com.cy.lib.download.Download.OnDownloadListener;
import com.cy.lib.download.DownloadService.MyBinder;
import com.cy.src.update.download.Config;
import com.cy.src.update.parser.DefaultXmlInfo;

import zhoufu.sz.sanxiao_check.utils.ImagePickerProvider;

public class CheckUpdateActivity extends FragmentActivity implements OnClickListener, OnCancelListener, ServiceConnection, OnDownloadListener {
    Config mConfig;
    DefaultXmlInfo mXmlInfo;
    MyBinder mBinder;
    ProgressDialog mProgressDialog;

    public CheckUpdateActivity() {
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mConfig = (Config)this.getIntent().getParcelableExtra("config");
        this.mXmlInfo = (DefaultXmlInfo)this.getIntent().getParcelableExtra("xmlInfo");
        this.showDialog(this.mXmlInfo.getAndroidcontent());
        this.bindDownloadService();
    }

    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(this);
        this.stopService(new Intent(this, DownloadService.class));
    }

    protected static void startActivity(Context context, Config config, DefaultXmlInfo xmlInfo) {
        Intent intent = new Intent(context, CheckUpdateActivity.class);
        intent.putExtra("config", config);
        intent.putExtra("xmlInfo", xmlInfo);
        context.startActivity(intent);
    }

    private void bindDownloadService() {
        Intent intent = new Intent(this, DownloadService.class);
        this.bindService(intent, this, 1);
    }

    private void showDialog(String content) {
        Builder builder = new Builder(this);
        AlertDialog dialog = builder.setTitle("提示").setMessage(content).setPositiveButton("更新", this).setNegativeButton("取消", this).create();
        dialog.setOnCancelListener(this);
        dialog.show();
    }

    public void onClick(DialogInterface dialog, int which) {
        switch(which) {
        case -2:
            this.finish();
            break;
        case -1:
            String downloadUrl = this.mXmlInfo.getAndroidurl();
            this.mBinder.startDownload(downloadUrl, this.mConfig.getLocalDownloadFile(), this);
        }

    }

    public void onCancel(DialogInterface dialog) {
        this.finish();
    }

    public void onServiceConnected(ComponentName name, IBinder service) {
        this.mBinder = (MyBinder)service;
    }

    public void onServiceDisconnected(ComponentName name) {
    }

    public void downloadState(DownloadState downloadState, DownloadInfo downloadInfo) {
        switch(downloadState) {
        case START:
            this.showProgress(downloadInfo);
            break;
          case PROGRESS:
            int c = (int)(downloadInfo.getCurrentProgress() * 100L / downloadInfo.getContentLength());
            this.mProgressDialog.setProgress(c);
            break;
          case COMPLETE:
            this.removeProgress();

            try {
                this.installApk(this, ImagePickerProvider.getProviderUri(this,downloadInfo.getTargetFile()));
            } catch (Exception var5) {
                var5.printStackTrace();
            }
            break;
          case ERROR:
            Toast.makeText(this, "下载出错", 0).show();
            this.removeProgress();
        }

    }

    private void showProgress(DownloadInfo downloadInfo) {
        this.mProgressDialog = new ProgressDialog(this);
        this.mProgressDialog.setProgressStyle(1);
        this.mProgressDialog.setProgress(0);
        this.mProgressDialog.setMax(100);
        this.mProgressDialog.setCancelable(false);
        this.mProgressDialog.setCanceledOnTouchOutside(false);
        this.mProgressDialog.setTitle("正在下载更新");
        this.mProgressDialog.show();
    }

    private void removeProgress() {
        this.mProgressDialog.dismiss();
        this.finish();
    }

    private void installApk(Context context, Uri uri) throws Exception {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setFlags(268435456);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}
